package com.imooc.demo.web;

import com.imooc.demo.entity.Order;
import com.imooc.demo.entity.Send;
import com.imooc.demo.service.SendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/send")
public class SendController {
    @Autowired
    SendService sendService;
    // 发送消息
    @RequestMapping(value = "allsends",method = RequestMethod.GET)
    private Map<String,Object> allSends(@RequestParam("buyerOpenid") String buyerOpenid){
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
        List<Send> sends=sendService.allSends(buyerOpenid);

        modelMap.put("sends",sends);
        return modelMap;
    }

    //设置消息阅读状态
    @RequestMapping(value = "/setreadstatus",method = RequestMethod.POST)
    //@RequestBody标识接收的内容除了xwww from url encoding 编码内容之外的一些内容，如json，xml等
    private  Map<String,Object> setReadStatus(@RequestParam("sendId") Integer sendId) {
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
       Send send = sendService.onlySendById(sendId);

        send.setIsRead(1);
        System.out.println(send);
        //修改种类信息 ，"success"保存操作状态，如category1Service.modifyCategory1(category1)操作成功返回true，否则false
        modelMap.put("success",sendService.setReadStatus(send));
        return modelMap;
    }

    //删除信消息
    @RequestMapping(value = "/deletesend",method = RequestMethod.POST)
    //@RequestBody标识接收的内容除了xwww from url encoding 编码内容之外的一些内容，如json，xml等
    private  Map<String,Object> deleteSend(@RequestParam("sendId") Integer sendId) {
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
        List<Send> sends = sendService.sendById(sendId);
        Send send =sends.get(0);
        send.setSendStatus(1);
        System.out.println(send);
        //修改种类信息 ，"success"保存操作状态，如category1Service.modifyCategory1(category1)操作成功返回true，否则false
        modelMap.put("success",sendService.deleteSend(send));
        return modelMap;
    }



    // //////查找某人所有未读信息
    @RequestMapping(value = "findunread",method = RequestMethod.GET)
    private Map<String,Object> findUnread(@RequestParam("buyerOpenid") String buyerOpenid){
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
        List<Send> sends=sendService.findUnread(buyerOpenid);

        modelMap.put("sends",sends);
        return modelMap;
    }


}
